package com.starter.home.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starter.home.dao.ServeTypeDao;
import com.starter.home.domain.entity.ServeType;
import com.starter.home.domain.vo.TypeVO;
import com.starter.home.exception.TypeException;
import com.starter.home.service.IServeTypeService;
import com.starter.home.util.OptionVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2020-04-30
 */
@Service
public class ServeTypeServiceImpl extends ServiceImpl<ServeTypeDao, ServeType> implements IServeTypeService {

    @Override
    public List<ServeType> getParentIds() {
        QueryWrapper<ServeType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",0);
        List<ServeType> serveTypes = this.baseMapper.selectList(queryWrapper);
        return serveTypes;
    }

    @Override
    public List<ServeType> getChildrenById(Integer id) {
        QueryWrapper<ServeType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        List<ServeType> serveTypes = this.baseMapper.selectList(queryWrapper);
        return serveTypes;
    }

    @Override
    public void deleteById(Integer id) {
        QueryWrapper<ServeType> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.eq("id",id).or().eq("parent_id",id);
        int i = this.baseMapper.delete(deleteWrapper);
        if ( i <= 0){
            throw new TypeException("删除失败！");
        }
    }

    @Override
    public List<OptionVO> getChildren() {
        QueryWrapper<ServeType> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("parent_id",0);
        List<ServeType> serveTypes = this.baseMapper.selectList(queryWrapper);
        List<OptionVO> list = new ArrayList<>();
        OptionVO vo = null;
        for (ServeType serveType : serveTypes) {
            vo = new OptionVO();
            vo.setId(serveType.getId());
            vo.setName(serveType.getTypeName());
            list.add(vo);
        }
        return list;
    }

    @Override
    public ServeType findById(Integer id) {
        ServeType serveType = this.baseMapper.selectById(id);
        return serveType;
    }

    @Override
    public List<TypeVO> getList() {
        List<ServeType> parentList = getParentIds();
        TypeVO typeVO = null;
        List<TypeVO> result = new ArrayList<>();
        for (ServeType serveType : parentList) {
            typeVO = new TypeVO();
            BeanUtil.copyProperties(serveType,typeVO);
            List<ServeType> childrenList = getChildrenById(serveType.getId());
            List<TypeVO> childList = new ArrayList<>();
            TypeVO typeVO1 = null;
            for (ServeType type : childrenList) {
                typeVO1 = new TypeVO();
                BeanUtil.copyProperties(type,typeVO1);
                childList.add(typeVO1);
            }
            typeVO.setChildren(childList);
            result.add(typeVO);
        }
        return result;

    }
}
